package cn.virens.common.spring.annotations.config;

import org.dromara.hutool.core.map.MapUtil;
import org.springframework.context.annotation.ImportAware;
import org.springframework.core.type.AnnotationMetadata;

import java.lang.annotation.Annotation;
import java.util.Map;

public abstract class VirAbstractImportAware implements ImportAware {
    protected Map<String, Object> attributes;

    /**
     * 获取注解全部参数
     */
    protected Map<String, Object> getAnnotationAttributes(AnnotationMetadata importMetadata) {
        return importMetadata.getAnnotationAttributes(getAnnotationClass().getName(), false);
    }

    /**
     * 获取注解类型
     */
    protected abstract Class<? extends Annotation> getAnnotationClass();

    @Override
    public void setImportMetadata(AnnotationMetadata importMetadata) {
        this.attributes = getAnnotationAttributes(importMetadata);
    }

    /**
     * 获取注解参数-Int
     */
    protected Integer getInteger(String key, Integer def) {
        return MapUtil.getInt(attributes, key, def);
    }

    /**
     * 获取注解参数-Str
     */
    protected String getString(String key, String def) {
        return MapUtil.getStr(attributes, key, def);
    }
}
